/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.student.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BizStudentMapper;
import com.nmf.dao.gen.bean.BizStudent;
import com.nmf.dao.gen.bean.BizStudentExample;
import com.nmf.site.common.enums.DelFlagEnums;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;

/**
 * 学生Service
 * @author 北冥
 * @version 2019-01-02
 */
@Service
@Transactional(readOnly = true)
public class BizStudentService{
	Logger log = LoggerFactory.getLogger(BizStudentService.class);
	@Autowired
	BizStudentMapper bizStudentMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizStudent get(String no) {
		return bizStudentMapper.selectByPrimaryKey(no);
	}
	
	public List<BizStudent> findList(BizStudent bizStudent) {
		BizStudentExample bizStudentExample = new BizStudentExample();
		BizStudentExample.Criteria criteria = bizStudentExample.createCriteria();
		
		return bizStudentMapper.selectByExample(bizStudentExample);
	}
	
	public BizStudent findByTelno(String telNo) {
		BizStudentExample bizStudentExample = new BizStudentExample();
		BizStudentExample.Criteria criteria = bizStudentExample.createCriteria();
		
		criteria.andTelNoEqualTo(telNo);
		
		List<BizStudent> blist = bizStudentMapper.selectByExample(bizStudentExample);
		if(blist.size()>0) {
			return blist.get(0);
		}
		return null;
	}
	
	public Page<BizStudent> findPage(Page<BizStudent> page, BizStudent bizStudent) {
		BizStudentExample bizStudentExample = new BizStudentExample();
		BizStudentExample.Criteria criteria = bizStudentExample.createCriteria();
		
		bizStudentExample.setPage(page);
		
		List<BizStudent> dlist = bizStudentMapper.selectByExample(bizStudentExample);
		page.setList(dlist);
		page.setCount(bizStudentMapper.countByExample(bizStudentExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BizStudent bizStudent) {
		if(StringUtils.isNotEmpty(bizStudent.getId())&&get(bizStudent.getId())!=null) {
			int optNum = bizStudentMapper.updateByPrimaryKeySelective(bizStudent);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizStudent.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizStudent.toString()+"】失败");
			}
		}else {
			Date date = new Date();
			bizStudent.setCreateDate(date);
			bizStudent.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value);
			bizStudent.setId(idGenerater.genClsNo("bizStudent"));
			bizStudent.setUpdateDate(date);
			int optNum = bizStudentMapper.insertSelective(bizStudent);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizStudent.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizStudent.toString()+"】失败");
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizStudent bizStudent) {
		bizStudentMapper.deleteByPrimaryKey(bizStudent.getId());
	}
	
}